﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsMstsc.Forms
{
    public partial class CtlTabPage : TabControl
    {

        public CtlTabPage() : base()
        {
        }
        public delegate void TagClose(object obj);
        public event TagClose OnTagClose;

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            Graphics g = e.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            Rectangle rect = this.GetTabRect(e.Index);
            TabPage page = this.TabPages[e.Index];
            SolidBrush brushBack;
            SolidBrush brushFront;
            string text = page.Text;
            //TabPage被选中时显示不同的前景色和背景色
            if (this.SelectedTab == page)
            {
                brushBack = new SolidBrush(Color.White);
            }
            else
            {
                brushBack = new SolidBrush(Color.FromArgb(240, 240, 240, 240));
            }
            brushFront = new SolidBrush(Color.Black);
            g.FillRectangle(brushBack, rect);
            Font font = this.Font;
            //Font font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            g.DrawString(text, font, brushFront, rect.X + 2, rect.Y + 5);
            g.DrawString("×", font, brushFront, rect.Right - 22, rect.Bottom - 18);
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
                return;
            TabPage page = this.SelectedTab;
            Rectangle rect = this.GetTabRect(this.SelectedIndex);
            Rectangle rect1 = new Rectangle(rect.Right - 22, rect.Y, 22, rect.Height);
            int x = e.X;
            int y = e.Y;
            //判断鼠标位置是否位于“❌”的范围内
            if (x > rect1.X && x < rect1.Right && y > rect1.Y && y < rect1.Bottom)
            {
                if (OnTagClose != null)
                {
                    OnTagClose(page);
                }
                this.TabPages.Remove(page);
            }
        }

    }
}
